// pages/logisticsAdd/logisticsAdd.js
import {queryGoods} from "../../api/aftersale.js";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        formData: {
            orderItemList: [],
            remark: ''
        }
    },

    /**
     * 移除故障产品
     */
    remove(event) {
        this.setData({
            'formData.orderItemList': this.data.formData.orderItemList.filter(item => item.proId != event.currentTarget.dataset.id)
        })
    },

    /**
     * 扫码添加信息
     */
    handleScanCode() {

        let _this = this
        wx.showLoading({
            title: '加载中',
        })
        wx.scanCode({
            scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
            complete(res) {
            },
            success(res) {
                queryGoods({"sn":res.result}).then(res => {
                    let {success, data} = res;
                    if (success) {
                        wx.hideLoading()
                        wx.navigateTo({
                            url: `/pages/selectParts/selectParts?proId=${data.id}&proName=${data.proBaseName}&code=${data.proBaseCode}&sn=${data.sn}`,
                            success: function (res) {
                                if (_this.data.formData){
                                    res.eventChannel.emit('inputSelectParts', {data: _this.data.formData})
                                }
                            }
                        })
                    }
                })
            },
            fail(err) {
                wx.showToast({
                    title: "扫描失败",
                    icon: 'none',
                    duration: 1500
                })
                wx.hideLoading()
            }
        })

    },
    /**
     * 选择配件
     */
    handleSelectParts() {
        let _this = this
        wx.navigateTo({
            url: '/pages/selectParts/selectParts',
            success: function (res) {
                res.eventChannel.emit('inputSelectParts', {data: _this.data.formData})
            }
        })
    },
    /**
     * 输入框改变
     */
    bindInputChange(e) {
        this.setData({
            [`formData.${e.currentTarget.id}`]: e.detail.value
        })
    },
    /**
     * 日期改变
     */
    bindDateChange(e) {
        this.setData({
            [`formData.${e.currentTarget.id}`]: e.detail.value
        })
    },
    /**
     * 取消
     */
    cancelBtn() {
        wx.navigateBack()
    },
    /**
     * 下一步
     */
    submitBtn(e) {

        if (this.data.formData.orderItemList.length <= 0) {
            wx.showToast({
                title: '请至少添加一个故障产品',
                icon: 'none'
            })
            return false;
        }
        if (!this.data.formData.remark) {
            wx.showToast({
                title: '请填写备注信息',
                icon: 'none'
            })
            return false;
        }
        let _this = this;
        wx.navigateTo({
            url: '/pages/logisticsAddInfo/logisticsAddInfo',
            success: function (res) {
                res.eventChannel.emit('acceptDataFromOpenerPage', {data: _this.data.formData})
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (option) {

        let _this = this

        const eventChannel = this.getOpenerEventChannel();

        if (Object.keys(eventChannel).length > 0) {

            eventChannel.on('acceptDataFromOpenerPage', function (data) {
                _this.setData({
                    'formData.orderItemList': data.data
                })
            });

            eventChannel.on('acceptDataFromOpenerPage2', function (data) {
                const dataInfo = data.data
                _this.setData({
                    [`formData.orderItemList`]: dataInfo.orderItemList,
                    [`formData.remark`]: dataInfo.remark,
                    [`formData.logisticsName`]: dataInfo.logisticsName,
                    [`formData.logisticsNo`]: dataInfo.logisticsNo,
                    [`formData.logisticsDate`]: dataInfo.logisticsDate,
                    [`formData.logisticsImg`]: dataInfo.logisticsImg
                })
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
